<%@ page language="java" import="java.sql.*, com.erclab.internal.xpresso.htmlgeneration.*, com.erclab.internal.xpresso.util.*" %>

<%@ include file="../jsp-includes/Header.jsp" %>

<%  String forwardUrl = request.getParameter("forwardUrl");
    String uniqueKeyName = request.getParameter("uniqueKeyName");
    String uniqueKeyValue = request.getParameter("uniqueKeyValue");
    String reportAction = request.getParameter("reportAction");
    String reportName = request.getParameter("reportName");
    String[] elements = request.getParameterValues("element");
    String modifiedElements = request.getParameter("modifiedElements");
    String sql = "";

    if (reportAction == null){
        throw new javax.servlet.jsp.JspException ("[103] No Report Action specified");
    }

    if (reportName == null){
        throw new javax.servlet.jsp.JspException ("[104] No reportName provided to process elements");
    }

    // Create the reportObject
    Report theReport = new Report(dbCon, reportName);

    String table = theReport.getTableAssociated();
    String reportCategory = theReport.getReportCategory();

    if (table == null){
        throw new javax.servlet.jsp.JspException ("[105] No Table Associated with this report");
    }

    if (reportAction.equalsIgnoreCase("Delete")){    
        if (elements != null){
            
            boolean proceed = false;
            // Check delete rules before executing any action
            try {
                proceed = theReport.runDeleteRules(elements, netId);
            } catch(Exception e) {
                throw new javax.servlet.jsp.JspException (e);
            }
            
            // Delete elements if proceed is true
            if (proceed){
                for(int i=0;i< elements.length;i++){
                    sql = "DELETE FROM " + table + " WHERE " + uniqueKeyName + "=" + elements[i];
                    dbCon.execInsert(sql, netId); 
                }
            } else {
                throw new javax.servlet.jsp.JspException ("[109] Delete cannot proceed. Data relationships would be broken");
            }
         }
    } else if (reportAction.equalsIgnoreCase("Update") && modifiedElements != null){
        // We need to get the ID's of the elements modified:
        modifiedElements = modifiedElements.substring(0, modifiedElements.length() - 1);
        String updateElements[] = modifiedElements.split(" ");

        // Get the editable elements on the report
        String[] columnsToEdit = theReport.getColumnsToEdit();
        String newValue;

        // Create an object to access the strings utilities
        ManStrings myStrings = new ManStrings();

        if (updateElements != null){
            for (int i = 0; i < updateElements.length; i++){
                // Update each element
                if (columnsToEdit != null){
                    sql = "UPDATE " + table + " SET ";
                    for (int j = 0; j < columnsToEdit.length; j++){
                        newValue = request.getParameter(columnsToEdit[j] + ":" + updateElements[i]);
                        newValue = newValue.replaceAll("\\$", "&#0036;");
                        System.out.println("Replacing dollar");
                        if (myStrings.isNumeric(newValue)){
                            sql += columnsToEdit[j] + "=" + newValue.replaceAll("'", "''") + ", ";
                        } else {
                            sql += columnsToEdit[j] + "='" + newValue.replaceAll("'", "''") + "', ";
                        }
                        //out.print("Column :" + columnsToEdit[j] + " - Value: " + request.getParameter(columnsToEdit[j] + ":" + updateElements[i]));
                    }
                    sql = sql.substring(0, sql.length() - 2);
                    sql += " WHERE " + uniqueKeyName + "=" + updateElements[i];
                    dbCon.execInsert(sql, netId); 
                }
            }
        }
    }

    System.out.println("The for: " + forwardUrl);

%>

<jsp:forward page="<%=forwardUrl%>" />

<%@ include file="../jsp-includes/footer.jsp" %>








